package com.shxc.smarttrade.old.adapternew;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.bean.TradeAccountInfo;
import com.shxc.smarttrade.old.myinterface.RecyclerViewItemClickListener;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jcdev1 on 2017/5/10.
 */

public class TradeAccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<TradeAccountInfo.DataBean.UserAccountBean> userAccountBeanList;
    private RecyclerViewItemClickListener listener;

    public TradeAccountAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setDatas(List<TradeAccountInfo.DataBean.UserAccountBean> userAccountBeanList) {
        this.userAccountBeanList = userAccountBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PositionHistoryViewholder(mInflater.inflate(R.layout.adapter_tigerwit_explain_layout, parent, false));
    }

    public void setOnChangeAccountClickListener(RecyclerViewItemClickListener listener) {

        this.listener = listener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof PositionHistoryViewholder) {
            final TradeAccountInfo.DataBean.UserAccountBean ub = userAccountBeanList.get(position);
            ((PositionHistoryViewholder) holder).tvBuildTime.setText("创建时间：" + BaseUtils.getDateFromMillionSeconds(ub.getCreatedAt(), "yyyy-MM-dd HH:mm:ss"));
            if (ub.getAccType() == 0) {//模拟账户
                ((PositionHistoryViewholder) holder).tvBrokerName.setText("TradeMaster(模拟账户)");
                ((PositionHistoryViewholder) holder).ivBrokerHead.setImageResource(R.mipmap.trade_master_trade_account);
                ((PositionHistoryViewholder) holder).llMtf.setVisibility(View.GONE);
                ((PositionHistoryViewholder) holder).tvBrokerExplain.setText(mContext.getResources().getString(R.string.trade_master_explain));
                if (ub.isNowAccount()) {
                    ((PositionHistoryViewholder) holder).ivIcon.setImageResource(R.mipmap.current_account_icon);
                    ((PositionHistoryViewholder) holder).tvAccountStatus.setText("当前账户");
                    if (ub.isDestroy()) {//判断是否过期
                        ((PositionHistoryViewholder) holder).ivIcon.setVisibility(View.GONE);
                        ((PositionHistoryViewholder) holder).tvAccountStatus.setText("账户已失效");
                    }

                } else if (ub.isDestroy()) {//判断是否过期
                    ((PositionHistoryViewholder) holder).ivIcon.setVisibility(View.GONE);
                    ((PositionHistoryViewholder) holder).tvAccountStatus.setText("账户已失效");
                } else {
                    ((PositionHistoryViewholder) holder).ivIcon.setImageResource(R.mipmap.change_account_icon);
                    ((PositionHistoryViewholder) holder).tvAccountStatus.setText("切换账户");
                    ((PositionHistoryViewholder) holder).llCurrentAccount.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            listener.onItemCLick(position, ub);
                        }
                    });
                }
            } else if (ub.getAccType() == 1) {//TigerWit账户

                ((PositionHistoryViewholder) holder).tvBrokerName.setText("TigerWit(" + ub.getMt4Id() + ")");
                ((PositionHistoryViewholder) holder).ivBrokerHead.setImageResource(R.mipmap.tiger_wit_icon);
                ((PositionHistoryViewholder) holder).llMtf.setVisibility(View.VISIBLE);
                ((PositionHistoryViewholder) holder).tvMtfAccount.setText("MT4账号：" + ub.getMt4Id());
                ub.getPasswd();
                if (!TextUtils.isEmpty(ub.getPasswd()) && !ub.getPasswd().equals("null"))
                    ((PositionHistoryViewholder) holder).tvMtfPassword.setText("密码：" + ub.getPasswd());
                else
                    ((PositionHistoryViewholder) holder).tvMtfPassword.setText("密码：切换后显示");

                ((PositionHistoryViewholder) holder).tvBrokerExplain.setText(mContext.getResources().getString(R.string.tiger_wit_explain));
                if (ub.isNowAccount()) {
                    ((PositionHistoryViewholder) holder).ivIcon.setImageResource(R.mipmap.current_account_icon);
                    ((PositionHistoryViewholder) holder).tvAccountStatus.setText("当前账户");
                } else {
                    ((PositionHistoryViewholder) holder).ivIcon.setImageResource(R.mipmap.change_account_icon);
                    ((PositionHistoryViewholder) holder).tvAccountStatus.setText("切换账户");
                    ((PositionHistoryViewholder) holder).llCurrentAccount.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            listener.onItemCLick(position, ub);
                        }
                    });
                }
            }

        }

    }

    @Override
    public int getItemCount() {
        return userAccountBeanList == null ? 0 : userAccountBeanList.size();
    }

    public class PositionHistoryViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_broker_name)
        TextView tvBrokerName;
        @BindView(R.id.tv_build_time)
        TextView tvBuildTime;
        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_account_status)
        TextView tvAccountStatus;
        @BindView(R.id.ll_current_account)
        AutoLinearLayout llCurrentAccount;
        @BindView(R.id.iv_broker_head)
        ImageView ivBrokerHead;
        @BindView(R.id.tv_broker_explain)
        TextView tvBrokerExplain;
        @BindView(R.id.tv_mtf_account)
        TextView tvMtfAccount;
        @BindView(R.id.tv_mtf_password)
        TextView tvMtfPassword;
        @BindView(R.id.ll_mtf)
        AutoLinearLayout llMtf;

        PositionHistoryViewholder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            AutoUtils.autoSize(view);
        }
    }

}
